// pages/personal/commentlist/commentlist.js
var app = getApp();
var utils = require('../../../utils/util.js');
Page({
  data: {
    data: [],
    busy: true,
  },
  myevent: function(e) {
    this.setData({
      data: e.detail
    })
  },
  onLoad: function(options) {
    var that = this;
    that._setConfig(function() {
      that._load();
    });
  },
  _load: function() {
    var that = this;
    if (that.data.config.show_finish) return false;
    that.setData({
      ['config.show_loading']: true,
    });
    var current_page = that.data.config.current_page;
    utils.http('/api/v1/member/my_comments_list', {
      page: current_page
    }, function(res) {
      console.log(res)
      var setData = {
        data: that.data.data.concat(res.datas),
        ['config.show_loading']: false,
        busy: false
      };
      if (res.datas.length == 0 && current_page == 1) {
        setData['config.show_empty'] = true;
      } else if (res.datas.length < 10) {
        setData['config.show_finish'] = true;
      }
      that.setData(setData);
    });
  },
  _setConfig(callback = '') {
    var that = this;
    that.setData({
      data: [],
      config: {
        current_page: 1,
        loading_msg: '加载中……',
        finish_msg: '就这么多了……',
        empty_msg: '暂无数据',
        show_loading: false,
        show_finish: false,
        show_empty: false,
      },
    }, function() {
      typeof callback == 'function' ? callback() : ''
    });
  },
  onPullDownRefresh: function() {
    this.onLoad();
    setTimeout(function() {
      wx.stopPullDownRefresh();
    }, 800)
  },
  onReachBottom: function() {
    var that = this;
    if (that.data.busy == true) return false;
    that.setData({
      busy: true,
      ['config.current_page']: that.data.config.current_page + 1
    }, function() {
      console.log('toLow');
      that._load();
    });
  },
  onShareAppMessage: function() {

  }
})